package sise.gr1.world.agent.logic;

import static org.junit.Assert.*;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.junit.BeforeClass;
import org.junit.Test;

import CLIPSJNI.Environment;
import CLIPSJNI.MultifieldValue;

public class WalczakDigger {

	private static final Logger logger = Logger
			.getLogger(WalczakDigger.class);

	private static final String logicPath = "rules/dWalczak.clp";
	private static Environment clips;

	@BeforeClass
	public static void init() {
		logger.info("init invoked");
		String resourcePath = "/src/main/resources/";
		String path = "";
		try {
			path = new java.io.File(".").getCanonicalPath();
		} catch (IOException e) {
			logger.error("Cannot create path to CLIPS file", e);
		}
		clips = new Environment();
		clips.load(path + resourcePath + logicPath);
	}

	@Test
	public void czyStojeNaOre() {
		clips.reset();
		clips.eval("(assert (visible (Object DIGGER) (PosX 1) (PosY 0)))");
		clips.eval("(assert (visible (Object DIGGER) (PosX 1) (PosY 1)))");
		clips.eval("(assert (field (Type ORE) (Resource 15) (PosX 0) (PosY 0)))");
		clips.eval("(assert (stat (Name boost) (Value 3)))");
		clips.run();
		MultifieldValue mv = (MultifieldValue) clips
				.eval("(find-all-facts ((?m move)) TRUE)");
		String result = "";
		try {
			result = mv.get(0).getFactSlot("Move").getValue().toString();
		} catch (Exception e) {
		}
		assertEquals(result, "collectOre");
	}
	
	@Test
	public void odpalBoost(){
		clips.reset();
//		clips.eval("(assert (visible (Object GUARD) (PosX 1) (PosY 0)))");
//		clips.eval("(assert (visible (Object GUARD) (PosX 1) (PosY 1)))");
		clips.eval("(assert (visible (Object DIGGER) (PosX 1) (PosY 0)))");
		clips.eval("(assert (visible (Object DIGGER) (PosX 1) (PosY 1)))");
		clips.eval("(assert (field (Type ORE) (Resource 15) (PosX 0) (PosY 0)))");
		clips.eval("(assert (stat (Name boost) (Value 0)))");
		clips.run();
		MultifieldValue mv = (MultifieldValue) clips
				.eval("(find-all-facts ((?m move)) TRUE)");
		String result = "";
		try {
			result = mv.get(0).getFactSlot("Move").getValue().toString();
		} catch (Exception e) {
		}
		assertEquals(result, "activateBoost");		
	}
	
	@Test
	public void jedz(){
		clips.reset();
		clips.eval("(assert (field (Type FOOD) (Resource 5) (PosX 0) (PosY 0)))");
		clips.eval("(assert (stat (Name boost) (Value 0)))");
		clips.eval("(assert (stat (Name food) (Value 3)))");
		clips.run();
		MultifieldValue mv = (MultifieldValue) clips
				.eval("(find-all-facts ((?m move)) TRUE)");
		String result = "";
		try {
			result = mv.get(0).getFactSlot("Move").getValue().toString();
		} catch (Exception e) {
		}
		assertEquals("collectFood", result);		
	}
	@Test
	public void idzDoNajlepszego(){
		clips.reset();
		for(int i=-2; i<=2; i++){
			for(int j=-2; j<=2; j++){
				if(i!=2 && j!=1){
					clips.eval("(assert (field (Type GROUND) (Resource 5) (PosX "+i+") (PosY "+j+")))");
				}else{
					clips.eval("(assert (field (Type GROUND) (Resource 5) (PosX 0) (PosY 0)))");
				}
			}
		}
		
		clips.eval("(assert (stat (Name boost) (Value 0)))");
		clips.eval("(assert (stat (Name food) (Value 3)))");
		clips.run();
		MultifieldValue mv = (MultifieldValue) clips
				.eval("(find-all-facts ((?m move)) TRUE)");
		String result = "";
		try {
			result = mv.get(0).getFactSlot("Move").getValue().toString();
		} catch (Exception e) {
		}
		assertEquals("move", result);		
	}

}
